package edu.luc.concordia;  

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.EventListenerList;

/** 
  * Copyright 2008 Project Concordia
 * <p>
 * This file is part of RetailReturnSystem.
 * <p>
 * RetailReturnSystem is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>.
 * <hr>
 * This Class shows System Summary & Errors when Initialization Done.
 * <p>
 * @author Rashmi Pandey & Piyushaben Desai
 * @version 0.1 2008-10-10
*/

public class SystemSummary extends JPanel implements ActionListener,PropertyChangeListener 
{
	//All Controls Declaration
	private JTextArea summaryTextArea = new JTextArea(20,30);
	private JButton cancelButton;
	private JButton startupButton;
	private JLabel summaryLabel;
	private JFrame frame;
	
	//Declare and Initialize Listeners
	private EventListenerList listener = new EventListenerList();
	private ActionListener forwardListener = new ActionListener()
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			Object[] currentListeners = listener.getListenerList();
			
			for (int i = currentListeners.length - 2; i >= 0; i -= 2) {
				if (currentListeners[i] == ActionListener.class) {
					((ActionListener) currentListeners[i + 1]).actionPerformed(e);
				}
			}
		}
		
	};
	public void addActionListener(ActionListener l)
	{
		listener.add(ActionListener.class, l);
	}
    
	/**
     * Constructor
     */
	public SystemSummary()
	{
		
	}
		
	public SystemSummary(JFrame frame)
	{
		this.frame = frame;
		this.setLayout(new BorderLayout());
				
		JPanel displayPanel = new JPanel();
		displayPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		summaryLabel = new JLabel ("Summary of Errors & Results:");
		displayPanel.add(summaryLabel);
		
		//Text Area
		summaryTextArea.setEditable(false);
		
		setText("Welcome to Retail Return System \n" +"Created By: Team Concordia \n" +	
				"We have got Loads of Discounts\n" + "Enjoy your Shopping \n" + "HAVE A NICE DAY!\n" +
				DateUtils.now());
		
		//Scroll		
		JScrollPane commentScroll = new JScrollPane(summaryTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, 
		ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		displayPanel.add(commentScroll);
		
		//Button panel
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		//OK Button
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener((ActionListener) this);
		buttonPanel.add(cancelButton);
		cancelButton.setActionCommand("SummaryCancel");
		
		startupButton = new JButton("Start Up");
		startupButton.addActionListener((ActionListener) this);
		buttonPanel.add(startupButton);
		
		// When Statup Button call this fires to Translation
		startupButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
			//String scanCode = scanCodeField.getText();
			ActionEvent scanevent = new ActionEvent(summaryTextArea,0,ActionConstants.ACCEPT_INITIALIZE);
			forwardListener.actionPerformed(scanevent);
			}
		});
		
		this.add(displayPanel, BorderLayout.WEST);
		this.add(buttonPanel, BorderLayout.SOUTH);		
	}
	
	/**
	 * This function display output string in text area.
	 * @param str returns the output string
	 */
	public void setText(String str)
	{
		summaryTextArea.setText(str);
	}
	
	public void actionPerformed(ActionEvent e)
	{
	}
	
	/**
	 * Receiving Events from Translation
	 */
	@SuppressWarnings("unchecked")
	public void propertyChange(PropertyChangeEvent e) 
	{
	if(PropertyConstants.SYSTEM_INIT_SUMMARY.equals(e.getPropertyName()))
		{
			ArrayList<String> desc = (ArrayList<String>) e.getNewValue();
			summaryTextArea.setText("");
			
			for(String output : desc)
			{
				summaryTextArea.append(output+"\n");
			}
			
			
				
				
		} 		
	}
}
